%% get data
clear all;
clc; 
load('Index.mat');

l = 1650;
r = 1701;
data = VNINDEX(l:r)';
N = length(data);
%% predict price by kalman filter
[ predprice, vpredprice ] = predictprice_kalman(data);
%%
figure(1);
hold all;
plot(data);
plot(predprice);
legend('index', 'predicted index');
%% kalman band
kal_upper = predprice + (vpredprice.^(1/2)).*1.96;
kal_lower = predprice - (vpredprice.^(1/2)).*1.96;

%% test performance of stratgy use kalman band
onewaycost = 0;
[ kal_position ] = bandstrategy( kal_upper, kal_lower, data );
[ kal_equityend, kal_sharpe, kal_annually_compound_return] = TestPerformance( kal_position, data,onewaycost );

fprintf('kal_equity %.7f\n',kal_equityend);
fprintf('kal_sharpe %.7f\n',kal_sharpe);
fprintf('kal_annually_compound_return %.7f\n',kal_annually_compound_return);

%% compute bollinger band
period = 20;
[ bol_lower, bol_average, bol_upper ] = bollingerband( period, data);
[ bol_position ] = bandstrategy( bol_upper, bol_lower, data );

[bol_equityend,bol_sharpe, bol_annually_compound_return] = TestPerformance( bol_position, data, onewaycost );
fprintf('bol_equity %.7f\n',bol_equityend);
fprintf('bol_sharpe %.7f\n',bol_sharpe);
fprintf('bol_annually_compound_return %.7f\n',bol_annually_compound_return);

%% alpha indicator
optC1 = FindOptimizedC(predprice,vpredprice,data);
[ alpha1_position,alpha_ind ] = alphaindicator( predprice, vpredprice,data,optC1);
    [alpha1_equityend,alpha1_sharpe,  alpha1_annually_compound_return] = TestPerformance( alpha1_position, data, onewaycost );
fprintf('alpha1_equity %.7f\n',alpha1_equityend);
fprintf('alpha1_sharpe %.7f\n',alpha1_sharpe);
fprintf('alpha1_annually_compound_return %.7f\n',alpha1_annually_compound_return);

%% buy and hold 

buy_hold_position = ones(1,length(data));
[buy_hold_equityend, buy_hold_sharpe, buy_hold_annually_compound_return] = TestPerformance( buy_hold_position, data, onewaycost );

fprintf('\nbuy_hold_equity %.7f\n',buy_hold_equityend);
fprintf('buy_hold_sharpe %.7f\n',buy_hold_sharpe);
fprintf('buy_hold_annually_compound_return %.7f\n',buy_hold_annually_compound_return);

%% Kalman-MA strategy

kal_MA_position = zeros(1,N);
i = 2;
while(i<N)
    if(predprice(i-1)>=data(i-1) && predprice(i)<=data(i))
        kal_MA_position(i+1) = 1;
        i = i+1;
        while((predprice(i-1)>=data(i-1) || predprice(i)<=data(i)) && (i<N))
            kal_MA_position(i+1) = 1;
            i = i+1;
        end    
    else
        i = i+1;
    end
end

onewaycost = 0;
[kal_MA_equityend,kal_MA_sharpe,  kal_MA_annually_compound_return] = TestPerformance( kal_MA_position, data, onewaycost );
fprintf('\nkal_MA_equityend %.7f\n',kal_MA_equityend);
fprintf('kal_MA_sharpe %.7f\n',kal_MA_sharpe);
fprintf('kal_MA_annually_compound_return %.7f\n',kal_MA_annually_compound_return);

%% VMA strategy
ScriptMA;




